$(function () {
    queryAjaxAdvertList(1);

    $('body').on('click','#btn1,#btn2',function () {
        //点击搜索查询从第一页开始查询
        queryAjaxAdvertList(1);
    }).on('click','.look-view-advert',function () {
        var id = $(this).data('id');
        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>查看广告详情</strong>',
            area: winArea,
            content: baseloaction + "/admin/op/advert/showAdvertInfo?id="+id,
            btn: ['关闭'],
            yes: function (index, layero) {
                parent.layer.close(index);
            }
        });
    }).on('click',"#deteleBatch",function () {//批量删除广告
        var obj = getSelectRecord();
        if(obj.length<=0){
            icModale(0,"没有选择要删除的广告记录");
            return;
        }
        var ids = getSelectVals(obj);
        deleteAdvert(ids);
    }).on('click',"#recoveryBatch",function () {
        var obj = getSelectRecord();
        if(obj.length<=0){
            icModale(0,"没有选择要恢复的广告记录");
            return;
        }
        var ids = getSelectVals(obj);
        updateAdvertStatus(ids,1);
    }).on('click',"#frozenBatch",function () {
        var obj = getSelectRecord();
        if(obj.length<=0){
            icModale(0,"没有选择要冻结的广告记录");
            return;
        }
        var ids = getSelectVals(obj);
        updateAdvertStatus(ids,2);
    }).on('click',".remove-advert",function () {//删除广告
        var id = $(this).data('id');
        deleteAdvert(id);
    }).on('click',".update-advert-status",function () {//修改广告状态
        var id = $(this).data('id');
        var status = $(this).data('status');
        updateAdvertStatus(id,status);
    });

    /**
     * 更新广告状态
     * @param ids 广告ID串
     * @param status 状态，1正常，2冻结
     */
    function updateAdvertStatus(ids,status) {
        var message = '确认要禁用选中广告记录？';
        if(status==1){
            message = '确认要恢复选中广告记录？';
        }
        icModale(2, message, 3, function () {
            $.ajax({
                url: baseloaction + '/admin/op/advert/updateOpAdvertStatus',
                type:'post',
                dataType:'json',
                data:{'ids':ids,'status':status},
                success:function (result) {
                    if(result.success==true){
                        var arr = (ids+"").split(",");
                        $.each(arr,function (i,e) {
                            var aem  =$("#simple-status-"+e);
                            var statText = $("#advert-status-"+e);
                            if(status==1){
                                statText.html('<span class="label label-info">正常</span>');
                                aem.data('status',"2");
                                aem.attr("data-original-title","禁用");
                                aem.find(".fa-history").removeClass('fa-history').addClass('fa-unlock-alt');
                            }else if(status==2){
                                statText.html('<span class="label label-danger">禁用</span>');
                                aem.data('status',"1");
                                aem.attr("data-original-title","恢复");
                                aem.find(".fa-unlock-alt").removeClass('fa-unlock-alt').addClass('fa-history');
                            }
                        });
                    }else{
                        icModale(0, result.message);
                    }
                },
                error:function (error) {
                    icModale(0, "系统错误，修改广告状态失败");
                }
            });
        });
    }

    /**
     * 删除广告
     * @param ids 要删除的广告ID
     */
    function deleteAdvert(ids) {
        icModale(2, "确认要删除选中的广告记录？", 3, function () {
            $.ajax({
                url:baseloaction + '/admin/op/advert/deleteAdvertByIds',
                type:'post',
                dataType:'json',
                data:{"ids":ids},
                success:function (result) {
                    if(result.success){
                        var arr = (ids+"").split(",");
                        $.each(arr,function (i,e) {
                           $("#advert-tr-"+e).remove();
                        });
                    }else{
                        icModale(0,result.message);
                    }
                },
                error:function (error) {
                    icModale(0,"系统错误，广告记录删除失败");
                }
            });
        });
    }

    function getSelectRecord() {
        var obj = $("input[name='advertId']:checked");
        return obj;
    }
    function getSelectVals(list) {
        var ids = '';
        $.each(list,function (i,e) {
            if(i<list.length-1){
                ids+=$(e).val()+",";
            }else{
                ids+=$(e).val();
            }
        });
        return ids;
    }
});



/**
 * 获取广告列表页面
 * @param pageNo 当前页码
 */
function queryAjaxAdvertList(pageNo) {
    var params = $("#search-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/op/advert/queryAdvertListPage',//请求URL
        contextId: 'advert-list-context',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: queryAjaxAdvertList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'select-all-advert', //全选框ID
        callback: function () {//数据加载完成回调
            $("#totalNum").html("（共"+total+"个广告）");
        }
    };
    ajaxGetListData(obj)
}